package com.community.zhihuishequ.view.personnel;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.community.zhihuishequ.R;
import com.community.zhihuishequ.view.base.BaseActivity;
import com.community.zhihuishequ.widget.CalendarDialog;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 人员进出
 */
public class PersonnelListActivity extends BaseActivity {

    @BindView(R.id.appBarLayout)
    AppBarLayout appBarLayout;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.rv_car_list)
    RecyclerView rvCarList;
    @BindView(R.id.toolBar)
    Toolbar toolBar;
    @BindView(R.id.rl_search)
    RelativeLayout rlSearch;
    private ContentAdapter adapter;
    private List<PersonnelData> dataList;

    private String templateStartTime;
    private String templateEndTime;
    private List<PersonnelData> contentList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personnel_list);
        unbinder = ButterKnife.bind(this);
        initActionBar(toolBar, true);
        toolBar.setNavigationIcon(R.drawable.ic_back);

        initData();
        initList();
    }

    private void initData() {
        dataList = new ArrayList<>();
        String name = "张三";
        String unit = "12座16";
        for (int i = 0; i < 20; i++) {
            PersonnelData personnelData = new PersonnelData();
            personnelData.setName(name + String.valueOf(i));
            personnelData.setAccessUnit(unit + String.valueOf(i));
            personnelData.setComeTime(TimeUtils.getNowString());
            personnelData.setStatus(i % 6);
            dataList.add(personnelData);
        }
    }

    private void search(String startTime, String endTime, String content) {
        if (contentList == null) {
            contentList = new ArrayList<>();
            contentList.addAll(dataList);
        }
        List<PersonnelData> templateList = new ArrayList<>();
        for (PersonnelData personnelData : contentList) {
//            if (!StringUtils.isEmpty(startTime)) {
//                Date a = TimeUtils.string2Date(personnelData.getComeTime());
//                long b = a.getTime();
//                Date v = TimeUtils.string2Date(startTime);
//                long z = v.getTime();
//                if (TimeUtils.getFitTimeSpan(personnelData.getComeTime(), startTime, 5)) {
//                    continue;
//                }
//            }
//            if (!StringUtils.isEmpty(endTime)) {
//                if (TimeUtils.string2Date(personnelData.getComeTime()).getTime() > TimeUtils.string2Date(endTime).getTime()) {
//                    continue;
//                }
//            }
            if (!StringUtils.isEmpty(content)) {
                if (!personnelData.getAccessUnit().contains(content)) {
                    continue;
                }
            }
            templateList.add(personnelData);
        }
        dataList.clear();
        adapter.notifyDataSetChanged();
        dataList.addAll(templateList);
        adapter.notifyDataSetChanged();
    }

    private void initList() {
        adapter = new ContentAdapter(R.layout.item_personnel, dataList);
        rvCarList.setLayoutManager(new LinearLayoutManager(this));
        rvCarList.setAdapter(adapter);
    }

    @OnClick(R.id.rl_search)
    public void onViewClicked() {
        search(templateStartTime, templateEndTime, etSearch.getText().toString());
    }

    private class ContentAdapter extends BaseQuickAdapter<PersonnelData, BaseViewHolder> {

        public ContentAdapter(int layoutResId, @Nullable List<PersonnelData> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, PersonnelData item) {
            helper.setText(R.id.tv_name, item.getName());
            helper.setText(R.id.tv_come_time, "通过时间：" + item.getComeTime());
            helper.setText(R.id.tv_access_unit, "访问单元：" + item.getAccessUnit());
            if (item.getStatus() == 0) {
                helper.setImageResource(R.id.img_personnal, R.drawable.ic_personnal_expressage);
            } else if (item.getStatus() == 1) {
                helper.setImageResource(R.id.img_personnal, R.drawable.ic_personnal_maintain);
            } else if (item.getStatus() == 2) {
                helper.setImageResource(R.id.img_personnal, R.drawable.ic_persosnnal_water);
            } else if (item.getStatus() == 3) {
                helper.setImageResource(R.id.img_personnal, R.drawable.ic_personnal_gas);
            } else if (item.getStatus() == 4) {
                helper.setImageResource(R.id.img_personnal, R.drawable.ic_personnal_family);
            } else if (item.getStatus() == 5) {
                helper.setImageResource(R.id.img_personnal, R.drawable.ic_personnal_friend);
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        if (item.getItemId() == R.id.item_calendar) {
            new CalendarDialog.Builder(this)
                    .setRadioVisible(false)
                    .setCallback(new CalendarDialog.Callback() {
                        @Override
                        public void onConfirmContent(int type, String start, String endTime) {
                            templateStartTime = start;
                            templateEndTime = endTime;
                            search(start, endTime, etSearch.getText().toString());
                        }
                    })
                    .show();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_car, menu);
        return true;
    }

}
